<?php
class Codegen_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    
    function get($table,$fields,$where='',$perpage=10,$start=0,$one=false,$array=''){
        
        $this->db->select($fields);
        $this->db->from($table);
        $this->db->limit($perpage,$start);
        if($where){
        $this->db->where($where);
        }
       
        $query = $this->db->get();
        
        $result =  !$one  ? $query->result() : $query->row() ;
        
        return $result;
    }
    function get_join($table,$table2,$join_on,$fields,$where='',$perpage=10,$start=0,$one=false,$array=''){
        
        $this->db->select($fields);
        $this->db->from($table);
        $this->db->join($table2, $join_on);
        $this->db->limit($perpage,$start);
        if($where){
        $this->db->where($where);
        }
       
        $query = $this->db->get();
        
        $result =  !$one  ? $query->result() : $query->row() ;
        
        return $result;
    }
    function add($table,$data){
        $this->db->insert($table, $data);         
        if ($this->db->affected_rows() == '1')
		{
			return TRUE;
		}
		
		return FALSE;       
    }
    
    function edit($table,$data,$fieldID,$ID){
        
        $this->db->where($fieldID,$ID);
        $this->db->update($table, $data);
          
        if ($this->db->affected_rows() >= 0)
		{
          
			return TRUE;
		}
		
		return FALSE;       
    }
    
    function delete($table,$fieldID,$ID){
        $this->db->where($fieldID,$ID);
        $this->db->delete($table);
        if ($this->db->affected_rows() == '1')
		{
			return TRUE;
		}
		
		return FALSE;        
    }   
	
	function count($table){
		return $this->db->count_all($table);
	}
    public function getPublish() {
        
        $query = $this->db->query("select published from product_status group by published");
        if ($query->num_rows() > 0) {
            return $row = $query->result();
        }
    }
    public function getproduct() {
        
        $query = $this->db->query("SELECT * FROM (`product`) left join artist on product.artist=artist.artist_id LIMIT 10");
        if ($query->num_rows() > 0) {
            return $row = $query->result();
        }
    }
    public function get_sanpham_sukien(){
        $query = $this->db->query("SELECT `sanpham_sukien`.`sanpham_sukien_id`,sukien.tensukien,sukien.masukien,sanpham_sukien.giam_gia,sanpham.ma_sp,sanpham.hinh_sp FROM sanpham_sukien 
LEFT JOIN sukien ON sanpham_sukien.masukien = sukien.masukien 
LEFT JOIN sanpham ON `sanpham_sukien`.`sanpham_sukien_id` = sanpham.id");
        if ($query->num_rows() > 0) {
            return $row = $query->result();
        }
    }
    
}